home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / consume.lha / consume.s < prev    next >
Encoding:
Text File  |  1993-04-24  |  5.7 KB  |  196 lines

  1. * CONSUME.S
  2. * Steven Frank / Vision of Epsilon
  3. * $VER: consume v1.0 (24.04.93)
  4. *
  5. * Shows how to set up an input handler that consumes (mm, delicious) all 
  6. * keyboard and mouse activity.  Hit left mouse button to unlock everything.
  7.  
  8. ; -------------------------------------------------------------------------
  9.  
  10.         incdir    'df0:include/'
  11.         include    'exec/io.i'
  12.         include    'exec/exec_lib.i'
  13.         include    'devices/input.i'
  14.         include    'devices/inputevent.i'
  15.         include    'libraries/dosextens.i'
  16.         include    'libraries/dos_lib.i'
  17.  
  18. NULL        equ    0
  19.  
  20. CALLSYS        MACRO
  21.         movea.l    (4).w,a6
  22.         jsr    _LVO\1(a6)
  23.         ENDM
  24.  
  25. ; -------------------------------------------------------------------------
  26. ; This first hunk, handily based on the ProTracker Source, will run the next  
  27. ; code hunk in the background and release the CLI for other uses.             
  28.  
  29.         section    RunBackHunk,CODE
  30.  
  31. rb_HunkStart:    suba.l    a1,a1            ; a1 = 0 = find our task
  32.         CALLSYS    FindTask        ; Find our task/process
  33.         move.l    d0,a4            ; process in a4
  34.         moveq.l    #0,d0            ; no WB msg ptr
  35.         tst.l    pr_CLI(a4)        ; are we called from CLI?
  36.         bne.s    CalledFromCLI        ; yes, do other stuff
  37.         lea    pr_MsgPort(a4),a0    ; running from wb, otherwise
  38.         CALLSYS    WaitPort        ; wait for the WB msg
  39.         lea    pr_MsgPort(a4),a0    ;
  40.         CALLSYS    GetMsg            ; get the WB msg
  41.         
  42. CalledFromCLI:    move.l    d0,d7            ; d7 = WB msg
  43.         lea    DOSLib,a1        ; 'dos.library'
  44.         moveq.l    #0,d0            ; and ol' version
  45.         CALLSYS    OpenLibrary        ; open it
  46.         tst.l    d0            ; did we get it?
  47.         beq.s    QuickExit        ; I sure hope so!!
  48.         move.l    d0,_DOSBase        ; save dosbase ptr
  49.  
  50.         CALLSYS    Forbid            ; stop multitasking for a sec
  51.         move.l    #ProgName,d1        ; ptr to the name of the prog
  52.         moveq.l    #0,d2            ; zero out d2
  53.         move.b    LN_PRI(a4),d2        ; priority
  54.         lea    rb_HunkStart-4(pc),a0    ; get ptr to next hunk
  55.         move.l    (a0),d3            ; ptr to next segment
  56.         clr.l    (a0)            ; unlink segment
  57.         move.l    #4000,d4        ; stack size
  58.         CALLDOS    CreateProc        ; spawn the process
  59.         CALLSYS    Permit            ; multitasking on
  60.  
  61. QuickExit:    tst.l    d7            ; is there a WB msg?
  62.         beq.b    RBExit            ; no, skip this part
  63.         CALLSYS    Forbid            ; yes, multitask off
  64.         move.l    d7,a1            ; move msg ptr to a1
  65.         CALLSYS    ReplyMsg        ; reply to the message
  66. RBExit:        moveq.l    #0,d0            ; result code = 0
  67.         rts                ; program returns to DOS here
  68.         
  69. ; -------------------------------------------------------------------------
  70.  
  71.         section    Main,CODE
  72.         
  73. MainStart:    bra.w    OpenAll            ; open everything we need
  74.  
  75. main:        tst.b    Exit            ; are we done?
  76.         beq.b    main            ; no, so repeat
  77.  
  78.         bra.w    CloseAll        ; clean up everything
  79.         
  80. ; -------------------------------------------------------------------------
  81.  
  82. InputHandler:    movem.l    d1-d7/a0-a6,-(sp)
  83.  
  84.         move.l    a0,a1            ; save ptr to first event
  85. ih_Start:    cmpi.b    #IECLASS_RAWKEY,ie_Class(a0)
  86.         bne.b    NotAKey            ; it was not a rawkey event
  87.  
  88.         move.b    #IECLASS_NULL,ie_Class(a0)
  89.         bra.b    Next            ; look for next event
  90.  
  91. NotAKey:    cmpi.b    #IECLASS_RAWMOUSE,ie_Class(a0)
  92.         bne.b    Next            ; not a mouse event
  93.  
  94.         move.b    #IECLASS_NULL,ie_Class(a0)
  95.  
  96.         cmpi.w    #IECODE_LBUTTON,ie_Code(a0)
  97.         bne.b    Next
  98.  
  99.         st.b    Exit        
  100.  
  101. Next:        move.l    (a0),a0            ; get ptr to next event
  102.         cmp.l    #NULL,a0        ; is there a next event?
  103.         bne.b    ih_Start        ; yes, loop again
  104.         move.l    a1,d0            ; pass ptr to first event
  105.         movem.l    (sp)+,d1-d7/a0-a6    ;   in list to next handler
  106.         rts                ; end of interrupt
  107.  
  108. ; -------------------------------------------------------------------------
  109.  
  110. OpenAll:    suba.l    a1,a1            ; a1 = 0 = find our task
  111.         CALLSYS    FindTask        ; do it
  112.         lea    InputPort,a1        ; reply port for input device
  113.         move.l    d0,MP_SIGTASK(a1)    ; signal us when msg received
  114.         CALLSYS    AddPort            ; add the port to the system
  115.         
  116.         lea    InputDevName,a0        ; 'input.device'
  117.         lea    InputIOReq,a1        ; IOStdReq structure for it
  118.         move.l    #InputPort,MN_REPLYPORT(a1)
  119.         moveq.l    #0,d0            ; unit number (n/a)
  120.         moveq.l    #0,d1            ; flags (n/a)
  121.         CALLSYS    OpenDevice        ; try to open input.device
  122.         tst.l    d0            ; got it?
  123.         beq.b    NoError            ; yes, all is well
  124.         moveq.l    #1,d0            ; d0 = 1 = device error
  125.         bra.w    Error            ; shut down
  126. NoError:
  127.         lea    InputIOReq,a1        ; input.device io request
  128.         move.w    #IND_ADDHANDLER,IO_COMMAND(a1)
  129.         move.l    #InputInterrupt,IO_DATA(a1)
  130.         CALLSYS    DoIO            ; install my input handler
  131.                 
  132.         bra.w    main            ; resume with main program
  133.  
  134. ; -------------------------------------------------------------------------
  135.  
  136. CloseAll:    cmp.l    #1,d0            ; an opendevice error?
  137.         beq.b    DeviceError        ; yep
  138.  
  139.         lea    InputIOReq,a1        
  140.         move.w    #IND_REMHANDLER,IO_COMMAND(a1)
  141.         move.l    #InputInterrupt,IO_DATA(a1)
  142.         CALLSYS    DoIO            ; remove my input handler
  143.         lea    InputIOReq,a1        
  144.         CALLSYS    CloseDevice        ; close the input.device
  145.  
  146. DeviceError:    lea    InputPort,a1        ; ptr to the msgport
  147.         CALLSYS    RemPort            ; uninstall it
  148.  
  149. ca_1:        CALLSYS    Forbid
  150.         lea    MainStart-4(pc),a0    ; ptr to start of process code
  151.         move.l    a0,d1            ; move to d1
  152.         lsr.l    #2,d1            ; BCPL! Acckk!
  153.         CALLDOS    UnLoadSeg        ; free memory used by process
  154.         move.l    _DOSBase,a1
  155.         CALLSYS    CloseLibrary        ; close dos.library
  156.         CALLSYS    Permit
  157.         rts                ; exit the program
  158.  
  159. ; -------------------------------------------------------------------------
  160. ; Now HERE'S a happy little subroutine.
  161.  
  162. Error:        moveq.l    #-1,d0            ; repeat 65000+ times
  163. e_Loop:        move.w    #$f00,$dff180        ; flash screen red
  164.         dbra    d0,e_Loop        ; repeat
  165.         bra.w    CloseAll        ; shut ourself down
  166.  
  167. ; -------------------------------------------------------------------------
  168.  
  169.         section    TheData,DATA
  170.  
  171. Version:    dc.b    '$VER: consume v1.0 (24.04.93)',0
  172. ProgName:    dc.b    'Consume!',0
  173. DOSLib:        dc.b    'dos.library',0
  174. InputDevName:    dc.b    'input.device',0
  175.         even
  176.  
  177. InputInterrupt:    dc.l    NULL,NULL
  178.         dc.b    NT_INTERRUPT,52        
  179.         dc.l    InputInterruptName,NULL,InputHandler
  180. InputInterruptName:
  181.         dc.b    'Consume.Int',0
  182.         even
  183.  
  184. ; -------------------------------------------------------------------------
  185.  
  186.         section    TheBSS,BSS
  187.  
  188. _DOSBase:    ds.l    1
  189. InputIOReq:    ds.b    IOSTD_SIZE
  190. InputPort:    ds.b    MP_SIZE
  191. Exit:        ds.b    1
  192.         even
  193.  
  194. ; -------------------------------------------------------------------------
  195.  
  196.